package product

import (
	"context"
	"fmt"
	"net/http"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PriceRemoveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPriceRemoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PriceRemoveLogic {
	return &PriceRemoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PriceRemoveLogic) PriceRemove(req *types.ProductPriceRemoveRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)

	//1.查询产品价格是否存在
	var ids = make([]int64, 0)
	tx := l.svcCtx.DB.WithContext(l.ctx).Raw(fmt.Sprintf("select id from product_price where product_id=%d and price=%f", req.ProductId, req.Price)).Scan(&ids)
	if tx.Error != nil {
		fmt.Printf("[Error]删除产品[%d]价格[%f]:%s\n", req.ProductId, req.Price, err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	fmt.Printf("[Info]产品[%d]价格[%f]id:%#v\n", req.ProductId, req.Price, ids)

	//2.删除产品价格
	for _, id := range ids {
		err = l.svcCtx.ProductPriceModel.Delete(l.ctx, id)
		if err != nil {
			fmt.Printf("[Error]删除产品[%d]价格[%f][id:%d]:%s\n", req.ProductId, req.Price, id, err.Error())
			resp.Code = http.StatusInternalServerError
			resp.Msg = "服务内部错误"
			return resp, nil
		}
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
